package com.kitty.game.fight.service;

import com.kitty.common.model.Pos;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.fly.FlyTypeEnums;
import com.kitty.game.map.service.MapService;
import com.kitty.game.pet.service.PetService;
import com.kitty.game.role.message.RespAppear;
import com.kitty.game.role.message.RespUpdateAppearance;
import com.kitty.game.role.model.Role;
import com.kitty.game.team.model.Member;
import com.kitty.game.team.model.Team;
import com.kitty.game.team.service.TeamService;
import com.kitty.mina.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class NewBroadcastService {
    //禁止飞行器的地图ID集合
    private static List<Integer> notFeiXingSet = Arrays.asList(5015,37013);
    @Autowired
    TeamService teamService;
    @Autowired
    MapService mapService;
    @Autowired
    PetService petService;
    @Autowired
    EquipService equipService;
    /*
    重写刷新角色外观事件：因为总感觉现在的刷新角色外观是有问题的。
    尤其是飞行器代码。
     重写刷新外观。 分开多种刷新类型。 不在全部刷新
     */
    public Message getFeiXingUpdateAppear(Role role,RespUpdateAppearance respUpdateAppearance) {
        //取队伍
        Team team =teamService.getTeam(role.getRoleId());
        //好像要判断是否暂离？
        if(team != null && teamService.isInTeam(role)){
            int leaderId = team.getLeaderRoleId();
            Role leaderRole = SpringUtils.getRoleService().getPlayerBy(leaderId);
            respUpdateAppearance.setLeaderId(leaderId);
            getRoleUpdateAppear(leaderRole,respUpdateAppearance);
            if(respUpdateAppearance.getFly_type() >2){
                int count_n = 0 ;
                List<Integer> move_ids = new ArrayList<Integer>() ;
                for (Member member:team.getList()){
                    count_n  = count_n+ 1 ;
                    if(count_n <= 5 && member.isInTeam()){
                        move_ids.add(member.getRoleId()) ;
                    }
                }
                respUpdateAppearance.setMove_ids(move_ids);
            }
            respUpdateAppearance.setMoveSpeed((byte)leaderRole.getMoveSpeed());
        }else{
            getRoleUpdateAppear(role,respUpdateAppearance);
            if(respUpdateAppearance.getFly_type() >2){
                List<Integer> move_ids = new ArrayList<Integer>() ;
                move_ids.add(role.getRoleId()) ;
                respUpdateAppearance.setMove_ids(move_ids);
            }
            respUpdateAppearance.setMoveSpeed((byte)role.getMoveSpeed());
        }
        return respUpdateAppearance;
    }
    public void getRoleUpdateAppear(Role role,RespUpdateAppearance respUpdateAppearance){
        Pos position = role.getPos();
        int mapId = position.getMapId();
        //装备飞行器开关
        //role.getPropsStatus().setv("fly_status", 1);
        if(notFeiXingSet.contains(mapId)){
            //地图禁止飞行
            removeFeixingUpdateApper(respUpdateAppearance);
        }else{
            //地图可以飞行，在判断是否是打开飞行器状态
            int flyStatus = role.getPropsStatus().getInt("fly_status", 0);
            if(flyStatus == 1){
                removeFeixingUpdateApper(respUpdateAppearance);
            }else{
                addFeixingUpdateApper(role,respUpdateAppearance);
            }
        }
    }
    //装备飞行器
    public Message addFeixingUpdateApper(Role role,RespUpdateAppearance respUpdateAppearance) {
        RoleEquip roleEquip = equipService.getRoleEquipByPos(role, 40);
        if(roleEquip ==null){
            respUpdateAppearance.setMove_type((byte) 0);
            respUpdateAppearance.setFly_type((byte) 0);
            return respUpdateAppearance;
        }
        int type = FlyTypeEnums.getType(roleEquip.getName());
        if(type ==0){
            respUpdateAppearance.setMove_type((byte) 0);
        }else{
            respUpdateAppearance.setMove_type((byte) 1);
        }
        respUpdateAppearance.setFly_type((byte) type);
        return respUpdateAppearance;
    }
    //下掉飞行器
    public Message removeFeixingUpdateApper(RespUpdateAppearance respUpdateAppearance) {
        respUpdateAppearance.setMove_type((byte) 0);
        respUpdateAppearance.setFly_type((byte) 0);
        return respUpdateAppearance;
    }

    public Message getFeiXingAppear(Role role, RespAppear respAppear) {
        //取队伍
        Team team =teamService.getTeam(role.getRoleId());
        //好像要判断是否暂离？
        if(team != null && teamService.isInTeam(role)){
            int leaderId = team.getLeaderRoleId();
            Role leaderRole = SpringUtils.getRoleService().getPlayerBy(leaderId);
            respAppear.setLeaderId(leaderId);
            getRoleAppear(leaderRole,respAppear);
            if(respAppear.getFly_type() >2){
                int count_n = 0 ;
                List<Integer> move_ids = new ArrayList<Integer>() ;
                for (Member member:team.getList()){
                    count_n  = count_n+ 1 ;
                    if(count_n <= 5 && member.isInTeam()){
                        move_ids.add(member.getRoleId()) ;
                    }
                }
                respAppear.setMove_ids(move_ids);
            }
            respAppear.setMoveSpeed((byte)leaderRole.getMoveSpeed());
        }else{
            getRoleAppear(role,respAppear);
            if(respAppear.getFly_type() >2){
                List<Integer> move_ids = new ArrayList<Integer>() ;
                move_ids.add(role.getRoleId()) ;
                respAppear.setMove_ids(move_ids);
            }
            respAppear.setMoveSpeed((byte)role.getMoveSpeed());
        }
        return respAppear;
    }
    public void getRoleAppear(Role role, RespAppear respAppear){
        Pos position = role.getPos();
        int mapId = position.getMapId();
        //装备飞行器开关
        //role.getPropsStatus().setv("fly_status", 1);
        if(notFeiXingSet.contains(mapId)){
            //地图禁止飞行
            removeFeixingApper(respAppear);
        }else{
            //地图可以飞行，在判断是否是打开飞行器状态
            int flyStatus = role.getPropsStatus().getInt("fly_status", 0);
            if(flyStatus == 1){
                removeFeixingApper(respAppear);
            }else{
                addFeixingApper(role,respAppear);
            }
        }
    }
    //装备飞行器
    public Message addFeixingApper(Role role, RespAppear respAppear) {
        RoleEquip roleEquip = equipService.getRoleEquipByPos(role, 40);
        if(roleEquip ==null){
            respAppear.setMove_type((byte) 0);
            respAppear.setFly_type((byte) 0);
            return respAppear;
        }
        int type = FlyTypeEnums.getType(roleEquip.getName());
        if(type ==0){
            respAppear.setMove_type((byte) 0);
        }else{
            respAppear.setMove_type((byte) 1);
        }
        respAppear.setFly_type((byte) type);
        return respAppear;
    }
    //下掉飞行器
    public Message removeFeixingApper( RespAppear respAppear) {
        respAppear.setMove_type((byte) 0);
        respAppear.setFly_type((byte) 0);
        return respAppear;
    }
}
